package com.multidisplay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.multidisplay.bo.ResultBo;
import com.multidisplay.mapper.UserMapper;
import com.multidisplay.po.BaseData;
import com.multidisplay.po.User;
import com.multidisplay.pojo.AddUser;
import com.multidisplay.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Service("UserService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public ResultBo<User> findAll(int page, int limit){
        IPage iPage = new Page(page,limit);
        IPage<User> userIPage = userMapper.selectPage(iPage,null);
        ResultBo<User> resultBo = new ResultBo<>();
        resultBo.setCode(0);
        resultBo.setMsg("");
        resultBo.setCount(userIPage.getTotal());
        resultBo.setData(userIPage.getRecords());
        return resultBo;
    }

    @Override
    public List<User> findByuId(String uId) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("u_id",uId);
        List<User> users = userMapper.selectList(queryWrapper);
        return users;
    }

    @Override
    public int addUser(AddUser addUser){
            User user = new User(addUser);
                QueryWrapper<User> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("u_id", addUser.getuId());
                List<User> users = userMapper.selectList(queryWrapper);
                if(!users.isEmpty()) {
                    return -1;
                }
                try {
                    Date date = new Date();
                    user.setCreatetime(date);
                    userMapper.insert(user);
                    return 1;
                }catch (Exception e){
                    e.printStackTrace();
                    return 0;
                }
    }   //添加用户

    @Override
    public void deleteUser(String uId){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("u_id",uId);
        userMapper.delete(queryWrapper);
    }    //删除用户

    @Override
    public boolean updateUser(User user){
        try {
            userMapper.updateById(user);
        }
        catch (Exception e){
            return false;
        }
        return true;
    }    //更新用户信息

}
